package cn.hnu.data;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class UserData {
    private int id;
    private String name;
    private int age;
    private String registrationDate;
    //main方法
    public static void main(String[] args) throws IOException {
        //读取姓氏文件，生成姓氏列表
        List<String> xs = readNamesFromFile("data/姓氏.txt");
        //读取名字文件，生成名字列表
        List<String> ns = readNamesFromFile("data/名字.txt");
        //产生5000个用户
        List<UserData> users = generateUsers(xs, ns, 5000);
        //将用户数据写入文件
        saveUsersToCSV(users,"data/users_dataset.csv");
    }
    public UserData(int id, String name, int age, String registrationDate) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.registrationDate = registrationDate;
    }

    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getRegistrationDate() {
        return registrationDate;
    }

    // 1.从文件中读取姓名列表
    public static List<String> readNamesFromFile(String fileName) throws IOException {
        List<String> names = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        String line=null;
        while ((line=reader.readLine())!=null) {
            String[] nameArray = line.split(" ");
            for (String name :
                    nameArray) {
                names.add(name);
            }
        }
        reader.close();
        return names;
    }

    //2.随机生成用户数据
    public static List<UserData> generateUsers(List<String> surnames, List<String> givenNames, int count) {
        List<UserData> uds = new ArrayList<>();

        Random random = new Random();
        for (int i = 0; i < count; i++) {
            //从姓和名的list集合中，随机取出一个组合
            String surname = surnames.get(random.nextInt(surnames.size()));
            String givenName = givenNames.get(random.nextInt(givenNames.size()));
            String name = surname + givenName;
            int age = random.nextInt(60) + 18;  // 年龄范围为 18 至 78岁
            String registrationDate = generateRandomDate();  // 随机生成注册时间

            UserData ud = new UserData(i + 1, name, age, registrationDate);
            uds.add(ud);
        }

        return uds;
    }

    //3.生成随机日期（2020年之间）
    public static String generateRandomDate() {
        int year = 2020;
        int month = new Random().nextInt(12) + 1;  // 随机数0-11
        //   1-28
        int day = new Random().nextInt(28) + 1;  // 随机数0-27   //问题：没有29、30、31
        return year + "-" + month + "-" + day;
    }

    //4.将用户数据保存为 CSV 文件
    public static void saveUsersToCSV(List<UserData> uds, String fileName) throws IOException {
        FileWriter fw = new FileWriter(fileName);
        // 写入 CSV 表头
        //fw.write("uid,username,uage,regtime\n");

        // 逐行写入用户数据
        for (UserData ud : uds) {
            fw.write(ud.getId() + "," + ud.getName() + "," + ud.getAge() + "," + ud.getRegistrationDate() + "\n");
        }
        fw.close();
        System.out.println("用户数据已保存到 " + fileName);
    }

    @Override
    public String toString() {
        return "UserData{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", registrationDate='" + registrationDate + '\'' +
                '}';
    }
}